package com.ecarx.camera.worker.car.sensor;

import com.ecarx.camera.utils.LogUtils;
import com.ecarx.camera.worker.car.ICarSensorProxy;
import com.ecarx.camera.worker.car.aosp.AospCarManager;
import com.ecarx.camera.worker.car.constant.IllegalData;
import com.ecarx.xui.adaptapi.car.sensor.ISensor;
import com.ecarx.xui.adaptapi.car.sensor.ISensorEvent;

import java.util.ArrayList;
import java.util.List;

/**
 * Sensor相关管理类
 */
public class SensorManager {

    private static final String TAG = "SensorManager";

    private boolean hasRegisterListener = false;

    private final List<AccOnChangedListener> mAccOnChangedListeners = new ArrayList<>();

    private ICarSensorProxy.ISensorListener mSensorListener = new ICarSensorProxy.ISensorListener() {

        @Override
        public void onSensorEventChanged(int sensorId, int event) {
            boolean isAccOn = isAccOnValue(event);
            LogUtils.d(TAG, "onSensorEventChanged, sensorId:" + sensorId + ", event:" + event + ", isAccOn:" + isAccOn);
            for (AccOnChangedListener listener : mAccOnChangedListeners) {
                listener.onAccOnChanged(isAccOn);
            }
        }

        @Override
        public void onSensorValueChanged(int sensorId, float value) {
            LogUtils.d(TAG, "onSensorValueChanged, sensorId:" + sensorId + ", value:" + value);
        }

        @Override
        public void onSensorSupportedChanged(int sensorId, int functionStatus) {
            LogUtils.d(TAG, "onSensorSupportedChanged, sensorId:" + sensorId + ", functionStatus:" + functionStatus);
        }
    };

    private static class Holder {
        private static final SensorManager INSTANCE = new SensorManager();
    }

    public static SensorManager get() {
        return SensorManager.Holder.INSTANCE;
    }

    private SensorManager() {
    }

    /**
     * 初始化
     */
    public void init() {
        if (!hasRegisterListener) {
            boolean result = AospCarManager.get().getCarSensorProxy().registerListener(mSensorListener, ISensor.SENSOR_TYPE_IGNITION_STATE);
            LogUtils.d(TAG, "init, registerListener for id:" + ISensor.SENSOR_TYPE_IGNITION_STATE + ", result:" + result);
            if (result) {
                hasRegisterListener = true;
            }
        }
    }

    /**
     * 判断是否为上电状态
     * @return
     */
    public boolean isAccOn() {
        final int sensorId = ISensor.SENSOR_TYPE_IGNITION_STATE;
        int value = getSensorIntValue(sensorId);
        if (IllegalData.FunctionIllegalValue.CAR_PROPERTY_MANAGER_NOT_READY == value) {
            LogUtils.d(TAG, "isAccOn, sensorId:" + sensorId + ", value:" + value + ", carPropertyManager not ready, return true");
            return true;
        }
        boolean result = isAccOnValue(value);
        LogUtils.d(TAG, "isAccOn, sensorId:" + sensorId + ", value:" + value + ", result:" + result);
        return result;
    }

    /**
     * 注册AccOn状态变化监听
     * @param listener
     * @return
     */
    public boolean registerAccOnChangedListener(AccOnChangedListener listener) {
        return listener != null && !mAccOnChangedListeners.contains(listener) && mAccOnChangedListeners.add(listener);
    }

    /**
     * 取消注册AccOn状态变化监听
     * @param listener
     */
    public void unregisterAccOnChangedListener(AccOnChangedListener listener) {
        if (listener != null) {
            mAccOnChangedListeners.remove(listener);
        }
    }

    /**
     * 判断值是否为AccOn状态
     * @param value
     * @return
     */
    private boolean isAccOnValue(int value) {
        return ISensorEvent.IGNITION_STATE_ACC != value;
    }

    /**
     * 获取指定sensorId对应的value
     * @param sensorId
     * @return
     */
    private int getSensorIntValue(int sensorId) {
        return AospCarManager.get().getCarSensorProxy().getSensorEvent(sensorId);
    }

    /**
     * AccOn状态变化的监听
     */
    public interface AccOnChangedListener {
        /**
         * AccOn状态变化的回调
         * @param isAccOn 是否为AccOn状态
         */
        void onAccOnChanged(boolean isAccOn);
    }

}
